﻿@{
    Layout = null;
}
@model List<Authority.Entity.Modules>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" type="text/css" href="@Url.Content("~/Content/themes/dtree/dtree.css")" />
    <script type="text/javascript" src="@Url.Content("~/Scripts/common.js")"></script>
    <script type="text/javascript" src="@Url.Content("~/Scripts/jquery-1.6.min.js")"></script>
    <script type="text/javascript" src="@Url.Content("~/Scripts/dtree.js")"></script>
    <title>权限树</title>
</head>
<body>
    <div class="dtree">
        <p>
            <input type="button" value="保存" onclick="submitPermission()" />
        </p>
        <script type="text/javascript">
         $(function()
         {
            //选中岗位权限
             var permisionJson = eval("(@ViewBag.permisionJsonString)".replace(/&#39;/g,"'"));
             for(var p in permisionJson){
                //菜单
                $("input[id='modules"+permisionJson[p].modules+"']").attr("checked",true);
                //权限
                $("input[name='modules"+permisionJson[p].modules+"']").each(function(){
                    if((permisionJson[p].permisionTypes & $(this).val()) == $(this).val())
                    {
                        $(this).attr("checked",true);
                    }
                });
             }
            //选中用户权限

         });

        function submitPermission()
        {
            var permisionData ="";
            var i = 0;
            $("input[id*='modules']:checked").each(function(){
                    if(i !=0 ) permisionData+= "&";
                    permisionData +="Codes["+i+"]="+ $(this).val();
                    var permissionTypes = 0;
                    $("input[name='modules"+$(this).val()+"']:checked").each(function(){
                            permissionTypes = $(this).val() | permissionTypes;
                        });
                    permisionData += "&PermisionTypes["+i+"]=" + permissionTypes + "";
                    i++;
            });

            $.ajax({
                      type: "POST",
                      url:"@Url.Action("UpdatePermision")?PostId="+request("PostId")+"&UserId="+request("UserId"),
//                      contentType: "application/json", 
//                      dataType: "json",
                      data:  permisionData,
                      success: function(text){
                                alert(text);
//                              $.messager.show({
//				                    title:'岗位权限',
//				                    msg:text,
//				                    timeout:1500,
//				                    showType:'slide'
//			                    });
                      }
            });
        }

	    d = new dTree('d');

	    d.add("00000000-0000-0000-0000-000000000000", -1, '000', '全选');
        @foreach (var module in Model)
        {
            string controlNames = string.Empty;
            string PermisionTypes = string.Empty;
            if (@module.Controls != null && @module.Controls.Count != 0)
            {
                foreach (var ctl in @module.Controls)
                {
                    PermisionTypes += ctl.PermisionType.ToString() + "|";
                    controlNames += ctl.Name.ToString() + "|";
                }
                PermisionTypes = PermisionTypes.TrimEnd('|');
                controlNames = controlNames.TrimEnd('|');
            }
            if (string.IsNullOrEmpty(PermisionTypes))
            {
                <text>
                d.add("@module.Id", "@module.ParentId", "@module.Code","@module.Name");
                </text>
            }
            else
            {
                <text>
                d.add("@module.Id", "@module.ParentId", "@module.Code","@module.Name", "@controlNames","@PermisionTypes");
                </text>
            }
        }
 	    document.write(d);
	    d.openAll();
        </script>
    </div>
</body>
</html>
